/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import pe.gov.mtc.dgac.licencias.bean.ClienteTO;
import pe.gov.mtc.dgac.licencias.bean.ClienteoperadorTO;
import pe.gov.mtc.dgac.licencias.bean.FuncionTO;
import pe.gov.mtc.dgac.licencias.bean.HorasTO;
import pe.gov.mtc.dgac.licencias.bean.OperadorAereoTO;
import pe.gov.mtc.dgac.licencias.service.ClienteoperadorService;
import pe.gov.mtc.dgac.licencias.service.HorasService;

/**
 *
 * @author externo.gamaro
 */
public class HorasAction extends DispatchAction{
    private final static String SUCCESS = "success";
    private Log logger = LogFactory.getLog(this.getClass());
    private HorasService horasService;
     private ClienteoperadorService cliopeService;
     
    public HorasAction(HorasService horasService,ClienteoperadorService cliopeService) {
        super();
        this.horasService=horasService;
        this.cliopeService=cliopeService;
        
    }
    
     public ActionForward insertHoras(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("insertHoras");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String metodo = req.getParameter("metodo");
        String codcli = req.getParameter("cliente");
        String codcliop=req.getParameter("txtcliop");
        String ano= req.getParameter("txtano");
        String mes= req.getParameter("txtmes");
        String horaifr= req.getParameter("txthoraifr");
        String horadiurno= req.getParameter("txthoradiurno");
        String horanocturno= req.getParameter("txthoranocturno");
        String horainternac= req.getParameter("txthorainternac");
        
        try {
            HttpSession sesion = req.getSession();
               
               ClienteoperadorTO cliop=new ClienteoperadorTO();
               OperadorAereoTO op=new OperadorAereoTO();
               FuncionTO fnc=new FuncionTO();
               HorasTO  hora=new HorasTO();
               hora.setCod_cliente(Long.parseLong(codcli));
               hora.setCod_clie_oper(Long.parseLong(codcliop));
               hora.setAnoHora(Long.parseLong(ano));
               hora.setMesHora(Long.parseLong(mes));
               hora.setHoraIfr(Long.parseLong(horaifr));
               hora.setHoraDiurno(Long.parseLong(horadiurno));
               hora.setHoraNocturno(Long.parseLong(horanocturno));
               hora.setHoraInter(Long.parseLong(horainternac));
               hora.setUsuarioCrea("GAMARO");
                // parametros final
               
               String result=(String)(horasService.insertHoras(hora)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
                 cliop.setCodClieOper(0);
                 op.setCodOperador(0L);
                 cliop.setCodFuncion(fnc);
                 cliop.setOperadorAereo(op);
                 List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
                 sesion.setAttribute("listclienteoperador", listcliop);
                                 
                 msg="Registro Grabado";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
                                     
        }
       catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","insertHoras"));  
	}
        
        return mapping.findForward(forward);
        
    }  
   
     public ActionForward updateHoras(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("updateHoras");
        String forward="edit";
        ActionMessages errors = new ActionMessages(); 
        String metodo = req.getParameter("metodo");
         String codcli = req.getParameter("cliente");
         String codhora=req.getParameter("txtcodhora");
         String codcliop=req.getParameter("txtcliop");
         String ano= req.getParameter("txtano");
         String mes= req.getParameter("txtmes");
         String horaifr= req.getParameter("txthoraifr");
         String horadiurno= req.getParameter("txthoradiurno");
         String horanocturno= req.getParameter("txthoranocturno");
         String horainternac= req.getParameter("txthorainternac");
        try {
            HttpSession sesion = req.getSession();
            ClienteoperadorTO cliop=new ClienteoperadorTO();
            OperadorAereoTO op=new OperadorAereoTO(0L);
            FuncionTO fnc=new FuncionTO(0L);
            HorasTO  hora=new HorasTO();
            hora.setCodHoras(Long.parseLong(codhora));
            cliop.setCodClieOper(Integer.parseInt(codcliop));
            cliop.setCod_cliente(Long.parseLong(codcli));
            hora.setClienteoperador(cliop);
             //
           
            if(metodo.equals("edit")){
               System.out.println("Editando...");   
               hora.setAnoHora(Long.parseLong(ano));
               hora.setMesHora(Long.parseLong(mes));
               hora.setHoraIfr(Long.parseLong(horaifr));
               hora.setHoraDiurno(Long.parseLong(horadiurno));
               hora.setHoraNocturno(Long.parseLong(horanocturno));
               hora.setHoraInter(Long.parseLong(horainternac));
               hora.setUsuarioMod("GAMARO");
               // parametros final
               String result=(String)(horasService.updateHoras(hora)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 
                 msg="Registro Grabado";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
            }
            //cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
            //cliop.setCodClieOper(0);
            //op.setCodOperador(0L);
            cliop.setCodFuncion(fnc);
            cliop.setOperadorAereo(op);
            cliop=(ClienteoperadorTO)cliopeService.buscarClienteOperador(cliop).get(0);
            List<HorasTO> listhoras=horasService.selectHoras(hora);
            hora=listhoras.get(0);
            hora.setClienteoperador(cliop);
            sesion.setAttribute("clientehoras",hora);
            //getServletContext().getRequestDispatcher("/licencias/EditClienteHoras.jsp").forward(req, response);
       }
        catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","updateHoras"));  
	}
              
        return mapping.findForward(forward);
        
    }  
    public ActionForward deleteHoras(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("deleteHoras");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String metodo = req.getParameter("metodo");
        String codcli = req.getParameter("cliente");
        String codhora=req.getParameter("txtcodhora");
                        
        try {
            HttpSession sesion = req.getSession();
               HorasTO  hora=new HorasTO();
               ClienteoperadorTO cliop=new ClienteoperadorTO();
                OperadorAereoTO op=new OperadorAereoTO();
               cliop.setCod_cliente(Long.parseLong(codcli));
               hora.setCodHoras(Long.parseLong(codhora));
               FuncionTO fnc=new FuncionTO();
                // parametros final
               hora.setClienteoperador(cliop);
               String result=(String)(horasService.deleteHoras(hora)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
                 cliop.setCodClieOper(0);
                 op.setCodOperador(0L);
                 cliop.setCodFuncion(fnc);
                 cliop.setOperadorAereo(op);
                 List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
                 sesion.setAttribute("listclienteoperador", listcliop);
                 msg="Registro Eliminado..";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
                   
            //getServletContext().getRequestDispatcher("/licencias/HorasFormList.jsp").forward(req, response);
            
        }
       catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","deleteHoras"));  
	}
        
        return mapping.findForward(forward);
        
    }
    public ActionForward verHoras(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("verHoras");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
              
        try {
            HttpSession sesion = req.getSession();
               
               ClienteoperadorTO cliop=new ClienteoperadorTO();
               OperadorAereoTO op=new OperadorAereoTO();
               FuncionTO fnc=new FuncionTO();
               cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
               cliop.setCodClieOper(0);
               op.setCodOperador(0L);
               cliop.setCodFuncion(fnc);
               cliop.setOperadorAereo(op);
               List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
               sesion.setAttribute("listclienteoperador", listcliop);
                                     
        }
       catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","verHoras"));  
	}
        
        return mapping.findForward(forward);
        
    }  
}
